﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace Super.Common.Validate
{
    /// <summary> Ajax返回
    /// </summary>
    public class AjaxResult : ContentResult
    {
        private AjaxResult(string content)
        {
            Content = content;
        }

        /// <summary> 将当前页面替换为 xxx (不可回退)
        /// <param name="info">跳转前提示信息</param>
        /// </summary>
        public static AjaxResult Replace(string url, string info = null, int infoTimeout = 500)
        {
            if (info != null)
            {
                var code = "$.jBox.tip('" + info.ToScriptMessage() + "','success'); setTimeout(function(){" + " window.location.replace('{0}');".FormatMe(url) + "}," + infoTimeout + ");";
                return new AjaxResult(code);
            }
            return new AjaxResult("window.location.replace('{0}');".FormatMe(url));
        }

        /// <summary> 跳转到指定页
        /// </summary>
        public static AjaxResult Goto(string url)
        {
            return new AjaxResult("window.location = '{0}'".FormatMe(url)); ;
        }


        /// <summary> 弹出提示
        /// </summary>
        public static AjaxResult Tip(string text, TipType type = TipType.Info)
        {
            return new AjaxResult("$.jBox.tip('{0}','{1}')".FormatMe(text.ToScriptMessage(), type.ToString().ToLower()));
        }

        /// <summary> 自定义返回
        /// </summary>
        public static AjaxResult Customer(string content)
        {
            return new AjaxResult(content);
        }
    }

    /// <summary> TipType
    /// </summary>
    public enum TipType
    {
        Loading,
        Info,
        Success,
        Warning,
        Error
    }

}
